package com.z.kq.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.z.common.context.UserContext;
import com.z.common.web.vo.request.PageVo;
import com.z.kq.entity.OfficialBusiness;
import com.z.kq.mapper.OfficialBusinessMapper;
import com.z.kq.service.IOfficialBusinessService;
import com.z.sys.entity.Dept;
import com.z.sys.mapper.UserMapper;
import com.z.sys.service.IDeptService;
import com.z.sys.vo.UserInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公出Service业务层处理
 *
 * @author z
 * @date 2023-03-30
 */
@Service
public class OfficialBusinessServiceImpl extends ServiceImpl<OfficialBusinessMapper,OfficialBusiness> implements IOfficialBusinessService {


    @Resource
    private UserMapper userMapper;
    
    @Resource
    private IDeptService deptService;
    /**
     * 查询公出
     * @param officialBusiness 公出
     * @param pageVo
     * @return 公出 分页集合
     * */
    @Override
    public PageInfo<OfficialBusiness> selectOfficialBusinessPage(OfficialBusiness officialBusiness, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<OfficialBusiness> data = selectOfficialBusinessList(officialBusiness);
        return new PageInfo<>(data);
    }
    @Override
    public List<OfficialBusiness> selectOfficialBusinessList(OfficialBusiness officialBusiness) {
        if("1".equals(officialBusiness.getDeptId())){
            officialBusiness.setDeptId("");
        }
        if(StringUtils.isEmpty(officialBusiness.getDeptId())){
            int powerLevel = UserContext.getPowerLevel("考勤");
            if(powerLevel != 0){
                List<Dept> deptList = deptService.getDeptListByRole();
                String full_path_sql = "(";
                if(deptList != null && deptList.size() > 0){
                    for(Dept dept : deptList){
                        full_path_sql += ("d.full_path like '" + dept.getFullPath() + "/%' or d.full_path = '" + dept.getFullPath() + "' or ");
                    };

                }
                full_path_sql += " user_id = "+UserContext.currentUser().getId()+")";
                Map<String, Object> map = new HashMap<>();
                map.put("full_path_sql", full_path_sql);
                officialBusiness.setParams(map);
            }
        }
        return baseMapper.selectOfficialBusinessList(officialBusiness);
    }

    @Override
    public boolean save(OfficialBusiness entity) {
        if(StringUtils.isEmpty(entity.getUserId())){
            return false;
        }
        String[] ids = entity.getUserId().split(",");
        List<OfficialBusiness> list = new ArrayList<>();
        // 补充人员、机构信息
        for(String id : ids){
            OfficialBusiness o = new OfficialBusiness();
            BeanUtils.copyProperties(entity, o, "userId");
            UserInfoVo dbUserInfo = userMapper.selectUserInfoByUserId(Long.parseLong(id));;
            BeanUtils.copyProperties(dbUserInfo, o, "id");
            o.setUserId(id);
            list.add(o);
        }
        return saveBatch(list);
    }
}
